package com.itheima.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.PageResult;
import com.itheima.dto.Simple;
import com.itheima.dto.SubjectsTagsDto;
import com.itheima.mapper.SubjectsMapper;
import com.itheima.mapper.SubjectsTagsMapper;
import com.itheima.mapper.UserMapper;
import com.itheima.pojo.Subjects;
import com.itheima.pojo.SubjectsTags;
import com.itheima.pojo.User;
import com.itheima.service.SubjectsTagsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SubjectsTagsServiceImpl implements SubjectsTagsService {

    @Autowired
    private SubjectsTagsMapper subjectsTagsMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private SubjectsMapper subjectsMapper;

    @Transactional
    @Override
    public Integer addSubjectsTags(SubjectsTags subjectsTags) {

        subjectsTagsMapper.insert(subjectsTags);
        //先查出学科id,修改学科里面的标签数
        Subjects subjects = subjectsMapper.selectById(subjectsTags.getSubjectID());
        subjects.setTags(subjects.getTags()+1);
        subjectsMapper.updateById(subjects);
        return subjectsTags.getId();
    }

    @Override
    public void modifySubjectsTags(SubjectsTags subjectsTags) {
        subjectsTagsMapper.updateById(subjectsTags);
    }

    @Override
    public PageResult SubjectsTagsPage(Integer page, Integer pagesize, Integer subjectID, String tagName, Integer state) {

        IPage<SubjectsTags> p = new Page<>(page,pagesize);

        LambdaQueryWrapper<SubjectsTags> wapper = new LambdaQueryWrapper<>();
        wapper.eq(null!=subjectID, SubjectsTags::getSubjectID,subjectID)
                .like(null!=tagName && ""!=tagName, SubjectsTags::getTagName,tagName)
                .eq(null!=state, SubjectsTags::getState,state);
        IPage<SubjectsTags> subjectsTagsIPage = subjectsTagsMapper.selectPage(p, wapper);
        List<SubjectsTags> records = subjectsTagsIPage.getRecords();
        String s = JSON.toJSONString(records);
        List<SubjectsTagsDto> subjectsTagsDtos = JSON.parseArray(s, SubjectsTagsDto.class);
        subjectsTagsDtos.stream().forEach(c->{
            User user = userMapper.selectById(c.getCreatorID());
            c.setUsername(user.getUsername());
            Subjects subjects = subjectsMapper.selectById(c.getSubjectID());
            c.setSubjectName(subjects.getSubjectName());
        });
        return new PageResult((int)subjectsTagsIPage.getTotal(),pagesize, (int) subjectsTagsIPage.getPages(),page,subjectsTagsDtos);
    }

    @Transactional
    @Override
    public void removeSubjectsTagsById(Integer id) {
        //先查出学科id,修改学科里面的标签数
        SubjectsTags subjectsTags = subjectsTagsMapper.selectById(id);
        Subjects subjects = subjectsMapper.selectById(subjectsTags.getSubjectID());
        subjects.setTags(subjects.getTags()-1);
        subjectsMapper.updateById(subjects);
        //删除
        subjectsTagsMapper.deleteById(id);
    }

    @Override
    public void modifySubjectsTagsState(Integer id, Integer state) {
        SubjectsTags subjectsTags = new SubjectsTags();
        subjectsTags.setId(id);
        subjectsTags.setState(state);
        subjectsTagsMapper.updateById(subjectsTags);
    }

    @Override
    public SubjectsTagsDto findSubjectsTagsById(Integer id) {
        SubjectsTagsDto subjectsTagsDto = (SubjectsTagsDto) subjectsTagsMapper.selectById(id);
        User user = userMapper.selectById(subjectsTagsDto.getCreatorID());
        subjectsTagsDto.setUsername(user.getUsername());
        Subjects subjects = subjectsMapper.selectById(subjectsTagsDto.getSubjectID());
        subjectsTagsDto.setSubjectName(subjects.getSubjectName());
        return subjectsTagsDto;
    }

    @Override
    public List<Simple> subjectsTagsSimple(Integer subjectID) {
        List<Simple> simples =  subjectsTagsMapper.selectIdAndNameByid(subjectID);
        return simples;
    }
}
